<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template slot="left">
          <span>总记录数: 16 条</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="primary" size="small">导入excel</el-button>
          <el-button type="primary" size="small" @click="hExportExcel">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template slot-scope="scope">
              {{ scope.row.formOfEmployment | formatEmployment }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="账户状态" prop="enableState" />
          <el-table-column label="操作" fixed="right" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="hEditRole(scope.row)">角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :total="total"
            :current-page="params.page"
            :page-size="params.pageSize"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>

    <el-dialog title="新增员工" :visible.sync="showDialog">
      <add-employee @close="showDialog=false" @update-list="loadEmployeeList" />
    </el-dialog>

    <assign-role :user-id="userId" :show-role-dialog.sync="showRoleDialog" />
  </div>
</template>
<script>
import AddEmployee from './add'
import EmployeeEnum from '@/api/constant/employees'
const map = {}
EmployeeEnum.hireType.forEach(item => { map[item.id] = item.value })
import { reqGetEmployeeList, delEmployee } from '@/api/employees'
import PageTools from '@/components/PageTools'
import AssignRole from './assign-role.vue'
export default {
  name: 'Employees',
  components: {
    PageTools,
    AddEmployee,
    AssignRole
  },
  filters: {
    formatEmployment(val) {
      // console.log(val, map)
      return map[val]
    }
  },
  data() {
    return {
      list: [],
      params: {
        page: 1, // 当前页
        pageSize: 10 // 每页条数
      },
      total: 0, // 总数
      loading: false,
      showDialog: false,
      showRoleDialog: false,
      userId: null
    }
  },
  mounted() {
    this.loadEmployeeList()
  },
  methods: {
    hEditRole({ id }) {
      this.showRoleDialog = true
      this.userId = id
    },
    hExportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        // excel表示导入的模块对象
        // 1.准备表头数据
        const header = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '聘用形式': 'formOfEmployment',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '部门': 'departmentName'
        }
        const res = await reqGetEmployeeList(this.params)
        const data = res.rows.map(item => {
          return Object.values(header).map(key => item[key])
        })
        console.log(res.rows, data)

        excel.export_json_to_excel({
          header: Object.keys(header),
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    async hDel(id) {
      this.$confirm('你确认要删除么?', '温馨提示').then(async() => {
        await delEmployee(id)
        this.loadEmployeeList()
        this.$message.success('删除员工成功')
      }).catch(error => {
        console.log(error)
      })
    },
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.loadEmployeeList()
    },
    async loadEmployeeList() {
      this.loading = true
      console.log(this.params)
      try {
        // debugger
        const data = await reqGetEmployeeList(this.params)
        console.log(data)
        const { total, rows } = data
        this.total = total
        this.list = rows
      } catch (err) {
        console.log(err)
      }
      this.loading = false
    }
  }
}
</script>
